﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMSROUTER.Business;
using PluggableModulesInterface;
using StorageManagement;
using Settings;
using MTRouterBulkSmsViettel.ViettelWSBulkSms;
using System.Configuration;
using PluggableModulesRouter;
using SMSROUTER.StorageManagement;

namespace MTRouterBulkSmsViettel
{
    public class Router : WorkingBaseMTWebServiceRouter
    {
        #region Properties and Fields
        private WSBulkSms _SmsRouterObject = new WSBulkSms();
        protected override System.Web.Services.Protocols.SoapHttpClientProtocol SmsRouterObject
        {
            get
            {
                return _SmsRouterObject;
            }
        }
        
        public string CpCode
        {
            get
            {
                return GetConfig("CpCode");
            }
            set
            {
                SetConfig("CpCode", value);
            }
        }

        public string CommandCode
        {
            get
            {
                return GetConfig("CommandCode");
            }
            set
            {
                SetConfig("CommandCode", value);
            }
        }

        public string UserName
        {
            get
            {
                return GetConfig("UserName");
            }
            set
            {
                SetConfig("UserName", value);
            }
        }

        public string Password
        {
            get
            {
                return GetConfig("Password");
            }
            set
            {
                SetConfig("Password", value);
            }
        }

        public string ContentType
        {
            get
            {
                return GetConfig("ContentType");
            }
            set
            {
                SetConfig("ContentType", value);
            }
        }

        public string RequestID
        {
            get
            {
                return GetConfig("RequestID");
            }
            set
            {
                SetConfig("RequestID", value);
            }
        }

        private struct Result
        {
            public const long SUCCESS = 1;
            public const long FAIL = 0;
        }
        #endregion

        protected override void SendSms(List<SmsOutWaiting> smsOut)
        {
            int threadId = ThreadID;
            bool logToText = this.EnableLogToText;
            foreach (var item in smsOut)
            {
                try
                {
                    resultBO result = (SmsRouterObject as WSBulkSms).wsCpMt(UserName, Password, CpCode, RequestID, item.Subs, item.DestAddr, item.ShortCode, CommandCode, item.Message, ContentType);
                    if (result.result == Result.SUCCESS)
                    {
                        SmsOutReportStorage.EnqueueElementData(Key, new SmsOutReportStruct(item.RouterID, item.SmsOutGUID, item.CooperateGUID, DateTime.Now, DateTime.Now, item.RetryOnFailReqTimes, item.RetryOnTimeOutReqTimes, AppSetting.Status.SENT, threadId));
                    }
                    else
                    {
                        SmsOutReportStorage.EnqueueElementData(Key, new SmsOutReportStruct(item.RouterID, item.SmsOutGUID, item.CooperateGUID, DateTime.Now, DateTime.Now, item.RetryOnFailReqTimes, item.RetryOnTimeOutReqTimes, AppSetting.Status.FAIL, threadId));
                        Log.InsertLog(result.message, "ViettelWSBulkSms.WSBulkSms.wsCpMt", ObjectType, AppSetting.WarningType.mt_error, item.CooperateID);
                    }
                    if (logToText)
                    {
                        Log.LogToText(item.SmsOutGUID, result.message, AppSetting.RouterType.MTROUTER);
                    }
                }
                catch (Exception ex)
                {
                    HandleException<SmsOutWaiting>(ex, AppSetting.WarningType.mt_error, item);
                }
            }
        }
    }    
}
